//
//  MineCouponsTableViewCell.swift
//  LeTu
//
//  Created by g1 on 2023/12/11.
//

import UIKit

class MineCouponsTableViewCell: BaseTableViewCell {

    private let scaleW = 388.0
    private let scaleH = 114.0
    private var couponModel = MineCouponsModel()
    private var index: Int = 0
    var clickGeCouponHandle: ((Int, MineCouponsModel)->Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.useBtn.layerCornerRadius(14)
        }
    }
    static func getCellHeight() -> CGFloat {
        guard let backImg = UIImage(named: "coupon_bg") else { return 0.01 }
        return (screenWidth-10*2)*backImg.size.height/backImg.size.width
    }
    func setCouponModel(_ couponModel: MineCouponsModel, index: Int, useStatus: MineCouponsUseStatus) {
        
        self.index = index
        self.couponModel = couponModel
        
        priceLabel.attributed.text = "\("¥", .font(kSetPingFangBlod(20)))\(couponModel.money)"
        usePromptLabel.text = couponModel.useBrief
        couponNameLabel.text = couponModel.name
        expireTimeLabel.text = "有效期至：\(couponModel.endDate)"
        
        useBtn.isHidden = useStatus != .unGet
        
        var statusIconName = ""
        switch useStatus {
        case .unUse:
            statusIconName = "coupon_unuse"
        case .used:
            statusIconName = "coupon_used"
        case .expire:
            statusIconName = "coupon_expire"
        case .unGet: break
        }
        statusImgView.isHidden = statusIconName.count == 0
        statusImgView.image = statusIconName.count == 0 ? nil:UIImage(named: statusIconName)
        
    }
    
    // MARK: - UIButtonAction
    @objc private func useBtnAction() {
        guard let handle = clickGeCouponHandle else { return }
        handle(index, couponModel)
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(backImgView)
        backImgView.addSubview(leftContentView)
        leftContentView.addSubview(priceLabel)
        leftContentView.addSubview(priceDetailLabel)
        backImgView.addSubview(useBtn)
        backImgView.addSubview(statusImgView)
        backImgView.addSubview(usePromptLabel)
        backImgView.addSubview(couponNameLabel)
        backImgView.addSubview(expireTimeLabel)

        let leftViewOriginW = 111.0
        backImgView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.top.bottom.equalToSuperview()
        }
        let backScale = (screenWidth-10*2)/scaleW
        leftContentView.snp.makeConstraints { make in
            make.left.equalTo(backScale*20)
            make.width.equalTo((leftViewOriginW-20*2)*backScale)
            make.centerY.equalToSuperview().offset(-3*backScale)
        }
        priceLabel.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
        }
        priceDetailLabel.snp.makeConstraints { make in
            make.centerX.equalTo(priceLabel)
            make.bottom.equalToSuperview()
            make.top.equalTo(priceLabel.snp.bottom)
        }
        usePromptLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset((leftViewOriginW+18)*backScale)
            make.centerY.equalTo(leftContentView).offset(-2)
        }
        couponNameLabel.snp.makeConstraints { make in
            make.left.equalTo(usePromptLabel)
            make.bottom.equalTo(usePromptLabel.snp.top).offset(-3*backScale)
        }
        expireTimeLabel.snp.makeConstraints { make in
            make.left.equalTo(usePromptLabel)
            make.top.equalTo(usePromptLabel.snp.bottom).offset(15*backScale)
        }
        useBtn.snp.makeConstraints { make in
            make.right.equalTo(-backScale*20)
            make.centerY.equalTo(leftContentView)
            make.width.equalTo(88)
            make.height.equalTo(28)
        }
        statusImgView.snp.makeConstraints { make in
            make.right.centerY.equalTo(useBtn)
            make.width.height.equalTo(54)
        }
        
        useBtn.addTarget(self, action: #selector(useBtnAction))
    }
    private lazy var backImgView: UIImageView = {
        let backImgView = UIImageView()
        backImgView.image = UIImage(named: "coupon_bg")
        backImgView.isUserInteractionEnabled = true
        return backImgView
    }()
    private lazy var leftContentView = UIView()
    private var priceLabel: UILabel = {
        let priceLabel = UILabel()
        priceLabel.font = kSetPingFangBlod(40)
        priceLabel.textColor = .app_priceTextColor
        priceLabel.textAlignment = .center
        return priceLabel
    }()
    private lazy var priceDetailLabel: UILabel = {
        let priceDetailLabel = UILabel()
        priceDetailLabel.font = kSetPingFangRegular(10)
        priceDetailLabel.textColor = .app_textGrayColor
        priceDetailLabel.textAlignment = .center
        return priceDetailLabel
    }()
    private lazy var usePromptLabel: UILabel = {
        let usePromptLabel = UILabel()
        usePromptLabel.font = kSetPingFangRegular(10)
        usePromptLabel.textColor = .app_textGrayColor
        return usePromptLabel
    }()
    private lazy var couponNameLabel: UILabel = {
        let couponNameLabel = UILabel()
        couponNameLabel.font = kSetPingFangRegular(15)
        return couponNameLabel
    }()
    private lazy var expireTimeLabel: UILabel = {
        let expireTimeLabel = UILabel()
        expireTimeLabel.font = kSetPingFangRegular(10)
        expireTimeLabel.textColor = .app_textGrayColor
        return expireTimeLabel
    }()
    private lazy var statusImgView: UIImageView = {
        let statusImgView = UIImageView()
        statusImgView.contentMode = .scaleAspectFit
        return statusImgView
    }()
    private lazy var useBtn: GradientButton = {
        let useBtn = GradientButton()
        useBtn.setTitle("立即领取".localized)
        useBtn.gradientColors = UIColor.app_themeGradientColors
        useBtn.setFont(kSetPingFangBlod(12))
        return useBtn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
